//
//  SUViewController.m
//  RPN Calculator
//
//  Created by SunnyUp on 13-1-6.
//  Copyright (c) 2013年 SunnyUp. All rights reserved.
//

#import "SUViewController.h"
#import "SUBrain.h"

@interface SUViewController ()

@property (nonatomic) BOOL isUserInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) SUBrain *brain;

@property (nonatomic, strong) NSString *contextString;

@end


@implementation SUViewController

@synthesize display = _display;
@synthesize context = _context;

@synthesize isUserInTheMiddleOfEnteringANumber = _isUserInTheMiddleOfEnteringANumber;
@synthesize brain = _brain;

@synthesize contextString = _contextString;

- (SUBrain *) brain
{
    if(!_brain)
        _brain = [[SUBrain alloc] init];
    
    return _brain;
}

- (NSString *) contextString
{
    if(!_contextString)
        _contextString = [[NSString alloc] init];
    
    return _contextString;
}

- (void) setIsUserInTheMiddleOfEnteringANumber:(BOOL)isUserInTheMiddleOfEnteringANumber
{
    _isUserInTheMiddleOfEnteringANumber = isUserInTheMiddleOfEnteringANumber;
    
    [self.deleteButton setEnabled:_isUserInTheMiddleOfEnteringANumber];
}

- (IBAction)digitPressed:(UIButton *)sender
{
    if(self.isUserInTheMiddleOfEnteringANumber)
    {
        NSRange range = [self.display.text rangeOfString:@"."];
        if([sender.currentTitle isEqualToString:@"."] && (range.location != NSNotFound))
        {
            //which means the user is trying to type a float number with more than one ".", and that is not allowed
            // we just simply ignore the second "." here
            return;
        }
        self.display.text = [self.display.text stringByAppendingString:sender.currentTitle];
    }
    else
    {
        self.display.text = sender.currentTitle;
        self.isUserInTheMiddleOfEnteringANumber = YES;
    }
    
    //update context label
    //if there is a '=' in the context string, we just remove it
    self.context.text = self.contextString;
}

- (IBAction)enterPressed
{
    [self.brain pushOperand:[self.display.text doubleValue]];
    self.isUserInTheMiddleOfEnteringANumber = NO;
    
    //update context label
    self.contextString = [self.contextString stringByAppendingFormat:@" %@", self.display.text];
    self.context.text = self.contextString;
}

- (IBAction)operatorPressed:(UIButton *)sender
{
    if(self.isUserInTheMiddleOfEnteringANumber) [self enterPressed];
    double result = [self.brain performOperation:sender.currentTitle];
    self.display.text = [NSString stringWithFormat:@"%g", result];
    
    //update context label
    self.contextString = [self.contextString stringByAppendingFormat:@" %@", sender.currentTitle];
    self.context.text = [self.contextString stringByAppendingString:@"="];
}

- (IBAction)minusPressed:(UIButton *)sender {
    if(self.isUserInTheMiddleOfEnteringANumber)
    {
        //if the "+/-" button is pressed during user typing, it means he wants to minus the current typing number
        if([self.display.text hasPrefix:@"-"])
            self.display.text = [self.display.text substringFromIndex:1];
        else
            self.display.text = [NSString stringWithFormat:@"-%@", self.display.text];
    }
    else
    {
        //if the "+/-" button is not pressed during user typing, it means he wants to minus the current result number
        double result = [self.brain performOperation:sender.currentTitle];
        self.display.text = [NSString stringWithFormat:@"%g", result];
        
        //in this case, the "+/-" is defenitely a operation, so we need to add it to the history context
        //update context label
        self.contextString =  [self.contextString stringByAppendingString:@" +/-"];
        self.context.text = [self.contextString stringByAppendingString:@"="];
    }
}

- (IBAction)piPressed:(UIButton *)sender {
    if(self.isUserInTheMiddleOfEnteringANumber) [self enterPressed];
    [self.brain performOperation:sender.currentTitle];
    self.display.text = sender.currentTitle;
    
    //update context label
    self.contextString = [self.contextString stringByAppendingFormat:@" %@", sender.currentTitle];
}

- (IBAction)deletePressed:(UIButton *)sender {
    if(self.isUserInTheMiddleOfEnteringANumber)
    {
        if(self.display.text.length > 1)
        {
            self.display.text = [self.display.text substringToIndex:(self.display.text.length - 1)];
        }
        else
        {
            self.display.text = @"0";
            self.isUserInTheMiddleOfEnteringANumber = NO;
        }
    }
}

- (IBAction)clearPressed:(UIButton *)sender {
    [self.brain clear];
    self.display.text = @"0";
    self.contextString = @"";
    self.context.text = self.contextString;
    self.isUserInTheMiddleOfEnteringANumber = NO;
}

@end
